package test0ne.设计模式.迭代器;

import java.util.Iterator;

public class DrivingRecord {
    private int index = -1;

    private String[] records = new String[10];

    public void append(String record) {
        if(index==9) {
            index=0;
        }else {
            index++;
        }
        records[index] = record;
    }

    //循环数组并显示所有10条记录
    public void display() {
        for (int i = 0; i < 10; i++) {
            System.out.println(i+": "+records[i]);
        }
    }

    //按顺序从最新的开始倒序显示
    public void displayOrder() {
        for (int i = index, loopCount=0; loopCount<10;i= i==0? i=9:i-1, loopCount++) {
            System.out.println(records[i]);
        }
    }

    public Iterator<String> iterator() {
        return new Itr();
    }

    private class Itr implements  Iterator<String> {
        int cursor = index;
        int loopCount = 0;


        @Override
        public boolean hasNext() {
            return loopCount<10;
        }

        @Override
        public String next() {
            int i = cursor;
            if(cursor == 0) {
                cursor = 9;
            }else {
                cursor--;
            }
            loopCount++;
            return records[i];
        }
    }

}
